package com.sim.util;

import android.util.Log;

import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class LogHelper {
    private static final boolean IS_SAVE_LOG = false;
    private static final boolean FEATURE_LOG_ENABLE = true;
    private static final boolean ERROR_LOG_ENABLE = true;

    private static final String TAG = "HZSIM";
    private static final String LOG_SUFFIX = "_app.log";
    private static final int LOG_ERROR_VALUE = -1;

    private static SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat("yyyy/MM/dd-HH:mm:ss:SSS");
    private static SimpleDateFormat logName = new SimpleDateFormat("yyyy_MM_dd");
    private static BlockingQueue<String> resultQueue = new LinkedBlockingQueue<String>();
    private static LogThread logThread = null;

    static{
        if(IS_SAVE_LOG){
            startLogThread();
        }
    }

    public static void log(String msg) {
        String log = mSimpleDateFormat.format(new Date()) + " : " + TAG + " : " + msg;
        if (FEATURE_LOG_ENABLE) {
            Log.e(TAG, msg);
        }
        addLog(log);
    }

    public static void log(String tag, String msg) {
        String log = mSimpleDateFormat.format(new Date()) + " : " + tag + " : " + msg;
        if (FEATURE_LOG_ENABLE) {
            Log.e(tag, msg);
        }
        addLog(log);
    }

    public static void log(Exception e) {
        String  errorInfo = e.toString();
        if(ERROR_LOG_ENABLE){
            errorInfo = errorToString(e);
        }
        String log = mSimpleDateFormat.format(new Date()) + " : " + TAG + " : " + errorInfo;

        if (FEATURE_LOG_ENABLE) {
            Log.e(TAG, errorInfo);
        }

        addLog(log);
    }

    public static void log(String msg, Exception e) {
        String  errorInfo = e.toString();
        if(ERROR_LOG_ENABLE){
            errorInfo = errorToString(e);
        }
        String log = mSimpleDateFormat.format(new Date()) + " : " + msg + " : " + errorInfo;

        if (FEATURE_LOG_ENABLE) {
            Log.e(TAG, errorInfo);
        }

        addLog(log);
    }

    public static void log(String tag, String msg, Exception e) {
        String  errorInfo = e.toString();

        if(ERROR_LOG_ENABLE){
            errorInfo = errorToString(e);
        }

        String log = mSimpleDateFormat.format(new Date()) + " : " + tag + " : " + msg + " : " + errorInfo;
        if (FEATURE_LOG_ENABLE) {
            Log.e(tag, msg, e);
        }

        addLog(log);
    }

    static class LogThread extends Thread {
        private boolean isRun = true;

        public void setRun(boolean run) {
            isRun = run;
        }

        @Override
        public void run() {
            while (isRun) {
                LogHelper.saveLog();
                pauseThread();
            }
        }

        /**
         * 暂停线程
         */
        protected void pauseThread() {
            try {
                synchronized (this) {
                    this.wait(10000);
                }
            } catch (Exception e) {
            }
        }

        /**
         * 继续线程
         */
        protected void resumeThread() {
            try {
                synchronized (this) {
                    this.notify();
                }
            } catch (Exception e) {
            }
        }
    }

    private static String errorToString(Exception e){
        StringWriter sw = new StringWriter();
        try(PrintWriter pw = new PrintWriter(sw);){
            e.printStackTrace(pw);
        }
        return sw.toString();
    }

    private static void addLog(String logMsg){
        if(IS_SAVE_LOG){
            resultQueue.add(logMsg);
        }
    }

    private synchronized static void startLogThread(){
        if(logThread == null){
            logThread = new LogThread();
            logThread.setRun(true);
            logThread.start();
        }
    }

    private static void saveLog() {
        try {
            StringBuilder sb = new StringBuilder();
            while (true) {
                String logRes = resultQueue.poll();
                if (logRes != null) {
                    sb.append(logRes);
                    sb.append("\n");
                } else {
                    break;
                }
            }
//            save(getBasePath(), sb.toString());
        } catch (Exception e) {
            LogHelper.log("saveLog Exception", e);
        }
    }

    private static boolean save(String file, String content) {
        boolean flag = false;
        try {
            FileWriter out = null;
            try {
                out = new FileWriter(file, true);
                out.write(content);
                out.flush();
            } catch (Exception e) {
                flag = false;
                LogHelper.log("save Exception", e);
            } finally {
                if (out != null) {
                    try {
                        out.close();
                    } catch (Exception e) {
                        Log.e(TAG,"save Exception", e);
                    }
                }
            }

            flag = true;
        } catch (Exception e) {
            Log.e(TAG,"save Exception", e);
        }
        return flag;
    }

    private static String getDataName(){
        return logName.format(new Date());
    }

}
